{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "level_set_visualization.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "_QO__KXIxS78"
      },
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "from matplotlib import cm\n",
        "import mediapy as media"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fiVMLQ0bxmXQ"
      },
      "source": [
        "def f(x, y):\n",
        "  r = np.sqrt(x**2 + y**2)\n",
        "  n = r*5+1\n",
        "  z = (np.abs(x)**n + np.abs(y)**n)**(1/n)\n",
        "  return z\n",
        "\n",
        "def g(x, y):\n",
        "  z = 1 - np.minimum(f(x - 0.5, y), f(x + 0.5, y))\n",
        "  return np.maximum(z, 0)\n",
        "\n",
        "n = 100\n",
        "x, y = np.meshgrid(np.linspace(-1.5, 1.5, 2*n), np.linspace(-1, 1, n), indexing='xy')\n",
        "plt.contourf(g(x, y))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Yv_ZrUADzXk7"
      },
      "source": [
        "\n",
        "n = 1000\n",
        "x, y = np.meshgrid(np.linspace(-1.5, 1.5, 2*n), np.linspace(-1, 1, n), indexing='xy')\n",
        "\n",
        "fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={\"projection\": \"3d\"})\n",
        "\n",
        "z = g(x, y)\n",
        "\n",
        "z0s = [0.2, 0.5, 0.8]\n",
        "colors = [cm.tab10(0), cm.tab10(1), cm.tab10(2)]\n",
        "\n",
        "ax.plot_surface(x, y, z, linewidth=0, antialiased=False, color='gray')\n",
        "\n",
        "x, y = np.meshgrid([-1.5, 1.5], [-1, 1], indexing='xy')\n",
        "xv = np.array([-1.5, -1.5, 1.5, 1.5, -1.5])\n",
        "yv = np.array([-1, 1, 1, -1, -1])\n",
        "\n",
        "for z0, color in zip(z0s, colors):\n",
        "  ax.plot3D(xv, yv, z0, zorder=10, color=color)\n",
        "\n",
        "ax.axis(False)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "d1pX7ypI3PE0"
      },
      "source": [
        "vis = []\n",
        "for z0, color in zip(z0s, colors):\n",
        "  mask = z > z0\n",
        "  vis.append(1-mask[:,:,None] * (1-np.array(color[:3])[None,None,:]))\n",
        "\n",
        "plt.figure(figsize=(8,8))\n",
        "plt.imshow(np.concatenate(vis[::-1], 0))\n",
        "plt.axis(False)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gS1-LG-u6xZs"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}